package 链表题;

import 单向链表.Test;

public class ListNode {
    public int val;
    public ListNode next;

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

 /*   @Override
    public String toString() {
        String str = "";
        while (next != null) {
            str = str + val + " ";
            next = next.next;
        }
        return str;
     }*/

    private static ListNode findLast(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode p;
        for (p = head; p.next !=null; p = p.next) {

        }
        return p;
    }

    public static ListNode of(int... value) {
        ListNode s = new ListNode(-1, null);
        for (int values : value) {
            ListNode last = ListNode.findLast(s);
            last.next = new ListNode(values, null);
        }
        return s.next;
    }

    @Override
    public String toString() {
        if (next == null) {
            return val + "";
        }
        return val + " " + next;
    }
}
